package dbsync4j.mapper.concrete.diff;

import java.util.ResourceBundle;

import dbsync4j.core.behavior.DatabaseObject;
import dbsync4j.mapper.behavior.diff.Difference;
import dbsync4j.utils.DBSync4JUtils;

public abstract class AbstractDifference implements Difference, Comparable<Difference> {

	protected ResourceBundle rb;
	protected DatabaseObject source;
	protected DatabaseObject destiny;
	protected String tag;
	protected String difference;
	protected String attribute;
	
	public AbstractDifference(DatabaseObject source, DatabaseObject destiny, String tag) {
		this.source = source;
		this.destiny = destiny;
		this.tag = tag;
		this.rb = DBSync4JUtils.RESOURCEBUNDLE;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		
		String fullNameSource = (source == null) ? rb.getString("null") : source.getFullName() ; 
		String fullNameDestiny = (destiny == null) ?  rb.getString("null") : destiny.getFullName() ; 
		
		String toString = rb.getString("tag") + ": " + rb.getString(this.tag);
		toString = toString + "\n" + rb.getString("difference")+ ": " + rb.getString("source") + " - " + fullNameSource + ", " +  rb.getString("destiny") + " - " + fullNameDestiny;
		
		return toString;
	}

	/* (non-Javadoc)
	 * @see dbsync4j.mapper.behavior.diff.Difference#getDestiny()
	 */
	@Override
	public DatabaseObject getDestiny() {

		return destiny;
	}

	/* (non-Javadoc)
	 * @see dbsync4j.mapper.behavior.diff.Difference#getSource()
	 */
	@Override
	public DatabaseObject getSource() {

		return source;
	}

	/* (non-Javadoc)
	 * @see dbsync4j.mapper.behavior.diff.Difference#getAttribute()
	 */
	@Override
	public String getAttribute() {
		
		return attribute != null ? this.rb.getString(attribute.replace("columnAttributes.", "")) : "-";
	}

	/* (non-Javadoc)
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	@Override
	public int compareTo(Difference o) {
		
		String name = o.getSource() == null ? (o.getDestiny().getName()) : o.getSource().getName();
		String name_ = source == null ? (destiny.getName()) : source.getName(); 
		//tableName.charAt(0) < tableName_.charAt(0) ? 1 : -1;
		return name_.compareTo(name);
	}

}
